package logic

import (
	"context"
	"fmt"
	"house/models/mysql"

	"house/houserpc/houserpc"
	"house/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseAddLogic {
	return &HouseAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseAddLogic) HouseAdd(in *houserpc.HouseAddRequest) (*houserpc.HouseAddResponse, error) {
	// todo: add your logic here and delete this line

	if in.Title == "" || in.HeadImage == "" || in.UserId == 0 || in.Context == "" || in.Price == 0 || in.Address == "" {
		return nil, fmt.Errorf("参数不完整，请继续添加")
	}
	house := mysql.House{
		Title:     in.Title,
		HeadImage: in.HeadImage,
		UserId:    int(in.UserId),
		Context:   in.Context,
		Price:     float64(in.Price),
		Address:   in.Address,
	}
	mysql.DB.Debug().Create(&house)
	return &houserpc.HouseAddResponse{
		HouseId: int64(house.ID),
	}, nil
}
